package utils

import (
	"math/rand"
	"time"
)

func GetRandSlices(length int, levels ...int) []int {
	level := 2
	if len(levels) > 0 && levels[0] < length {
		level = levels[0]
	}

	//点列表
	posLen := length / level
	posMod := length % level

	//实际等级
	rlevel := level
	if posMod > 0 {
		rlevel++
	}

	pos := make([][]int, rlevel)

	for i, _ := range pos {
		pos[i] = make([]int, 3)
		pos[i][0] = i * posLen //当前位置
		pos[i][1] = pos[i][0]  //起始位置，
		if i == rlevel-1 {
			pos[i][2] = length //结束位置
		} else {
			pos[i][2] = pos[i][0] + posLen //结束位置
		}
	}

	arr := make([]int, length)

	for i := 0; i < length; i++ {
		rr := rand.New(rand.NewSource(time.Now().UnixNano() - int64(i))).Intn(length)
		p := rr % rlevel
		arr[pos[p][0]] = i
		pos[p][0]++
		//位置使用完毕，删除
		if pos[p][0] >= pos[p][2] {
			pos = append(pos[:p], pos[p+1:]...)
			rlevel--
		}
	}
	return arr
}
